﻿using System;
using System.Collections.Generic;
using System.Linq; 
using MethodWorx.CMS.Content;
using System.Web.Mvc;
using MethodWorx.CMS.Requests;
using MethodWorx.CMS.Services;
using MethodWorx.Core.DomainModel;
using MethodWorx.Core.Validation;

namespace MethodWorx.CMS.UI.Areas.Application.Models
{
    public class CreateContentItemModel
    {
        public CreateContentItemRequest Request { get; set; }
        private SchemaService schema;
        private ContentService content;

        public CreateContentItemModel(SchemaService schema, ContentService content)
        {
            this.schema = schema;
            this.content = content;
        }

        public void Initialize(Guid id)
        {
            var page = this.content.GetPageById(id);
            if (page == null)
                throw new BusinessRuleException("Page Not Found");

            this.Request = new CreateContentItemRequest(page);
        }

        public SelectList GetContentTemplateSelectList()
        {
            return new SelectList(schema.GetAllContentTemplates(), "Id", "Name");
        }

        public void Complete()
        {
            this.content.CreateContentItem(this.Request);
        }

        internal void ValidatePublishedContentTemplates(CreateContentItemRequest createContentItemRequest, IValidationProvider validation)
        {
            this.content.ValidatePublishedContentTemplates(createContentItemRequest, validation);
        }
    }
}